//
//  FiveBtnMoreViewController.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/3.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "FiveBtnMoreViewController.h"
#import "FiveBtnMoreTableViewCell.h"

@interface FiveBtnMoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableViewDataArr;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger totalCount;//列表个数

@end

@implementation FiveBtnMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.title;
    [self.view addSubview:self.tableView];
    
    // 添加头部的下拉刷新
    MJRefreshNormalHeader *header = [[MJRefreshNormalHeader alloc] init];
    [header setRefreshingTarget:self refreshingAction:@selector(headerClick)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
    
    // 添加底部的上拉加载
    MJRefreshBackNormalFooter *footer = [[MJRefreshBackNormalFooter alloc] init];
    [footer setRefreshingTarget:self refreshingAction:@selector(footerClick)];
    self.tableView.mj_footer = footer;
    
//    self.tableView.rowHeight = UITableViewAutomaticDimension; // 自适应xib高度
//    self.tableView.estimatedRowHeight = 200; //先估计一个高度
}
// 头部的下拉刷新触发事件
- (void)headerClick {
    self.page = 1;
    self.pageSize = 5;
    
    [self loadNetData];
    // 结束刷新
    [self.tableView.mj_header endRefreshing];
}
// 底部的上拉加载触发事件
- (void)footerClick {
    self.page++;
    [self addData];
}

- (void)loadNetData {
    [SXLoadingView showProgressHUD:nil];
    if (self.tableViewDataArr.count != 0) {
        [self.tableViewDataArr removeAllObjects];
    }
    NSDictionary *par = @{@"page":@(self.page),
                          @"pageSize":@"5"
                          };
    NSString *str = URL_lzhImgNext;
    if ([self.type isEqualToString:@"夜上海"]) {
        str = URL_nightshImgNext;
    }
    [PPNetworkHelper POST:str parameters:par success:^(id responseObject) {
        [SXLoadingView hideProgressHUD];
        [self.tableViewDataArr addObjectsFromArray:responseObject[@"retBody"][@"rows"]];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [SXLoadingView showAlertHUD:SXLoadingErrorStr duration:SXLoadingErrorTime];
    }];
}

- (void)addData {
    NSDictionary *par = @{@"page":@(self.page),
                          @"pageSize":@"5"
                          };
    [PPNetworkHelper POST:URL_lzhImgNext parameters:par success:^(id responseObject) {
        NSArray *arr = responseObject[@"retBody"][@"rows"];
        self.totalCount = [responseObject[@"retBody"][@"total"] integerValue];
        if (IsNotNilArr(arr)) {
            [self.tableViewDataArr addObjectsFromArray:arr];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [SXLoadingView showAlertHUD:SXLoadingErrorStr duration:SXLoadingErrorTime];
    }];
}

#pragma mark --UITableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tableView.mj_footer.hidden = (self.tableViewDataArr.count == 0);
    if (self.tableViewDataArr.count == self.totalCount) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
    
    return self.tableViewDataArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if ([WXApi isWXAppInstalled]) {
        WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
        launchMiniProgramReq.userName = @"gh_bc51f92151c7";  //拉起的小程序的username-原始ID
        launchMiniProgramReq.path = self.tableViewDataArr[indexPath.row][@"appPath"];    //拉起小程序页面的可带参路径，不填默认拉起小程序首页
        launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //拉起小程序的类型
        [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
            
        }];
    } else {
        [SXLoadingView showAlertHUD:@"您还未安装微信~请先下载安装哦" duration:SXLoadingTime];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"FiveBtnMoreTableViewCell";
    FiveBtnMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[FiveBtnMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [cell updateFiveBtnMoreTableViewCellUI:self.tableViewDataArr[indexPath.row]];
    return cell;
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"FiveBtnMoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"FiveBtnMoreTableViewCell"];
    }
    return _tableView;
}


- (NSMutableArray *)tableViewDataArr {
    if (!_tableViewDataArr) {
        _tableViewDataArr = [[NSMutableArray alloc] init];
    }
    return _tableViewDataArr;
}

@end
